<?php

namespace Admin\Controller;

use Tools\AdminController,
    Model\News_contentsModel,
    Model\News_classModel,
    Tools\Page;

class NewsController extends AdminController {

    public function showlist() {
        //展示新闻列表
        $ncts = new News_contentsModel();
        //获得总记录数
        $total = $ncts->count();
        //每页显示条数
        $per = 7;

        $page  =  new Page($total, $per);
        $limit =  $page->getLimit();
        $ncts  -> limit($limit);
        $ncts  -> order('sort desc');
        $list  =  $ncts->select();
        $fpage = $page->fpage();
        $zlist = $this->news_class();

        $this -> assign('list', $list);
        $this -> assign('zlist', $zlist);
        $this -> assign('page', $fpage);
        $this -> display();
    }

    function add() {
        $news = new News_contentsModel();

        //获取新闻栏目名称
        $arr = $this->news_class();

        //收集添加信息，过滤，存入数据      
        if (!empty($_POST)) {
            if ($_POST['sort'] == "") {
                unset($_POST['sort']);
            }
            
            //上传文件处理
            if ($_FILES['picture']['error'] < 4) {
                $config = array(
                    'rootPath' => "Admin/Public/uploads/", //保存根路径
                    'maxSize' => 2000000, //上传的文件大小限制
                    'exts' => array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
                );
                $upload = new \Think\Upload($config);
                //文件上传保存，返回附件的相关信息
                $up = $upload->uploadOne($_FILES['picture']);
                if ($up) {

                    //图片上传成功后获得图片路径，并开始制作缩略图
                    $_POST['picture'] = 'http://' . $_SERVER['HTTP_HOST'] . '/Show/' . $upload->rootPath . $up['savepath'] . $up['savename'];
                }else{
                    $error = $upload -> getError();
                    $this->redirect('add', '', 2,$error);
                }
            }
            
            $shu = $news->create();
            if ($shu) {
                $shu['time'] = time();
                $shu['author'] = session('admin_name');
                if ($news->add($shu)) {
                    $this->redirect('showlist', '', 2, '发布成功');
                } else {
                    $this->redirect('add', '', 2, '发布失败');
                }
            } else {
                $error = $news->getError();
                $this->alert($error);
            }
        }

        $this->assign('news_class', $arr);
        $this->display();
    }

    function xiugai($id) {
        //修改信息
        $news = new News_contentsModel();
        $news->where("id=$id");
        $list = $news->find();
        $arr = $this->news_class();

        //收集信息并存储
        if (!empty($_POST)) {
             //上传文件处理
            if ($_FILES['picture']['error'] < 4) {
                $config = array(
                    'rootPath' => "Admin/Public/uploads/", //保存根路径
                    'maxSize' => 2000000, //上传的文件大小限制
                    'exts' => array('jpg', 'gif', 'png', 'jpeg'), //允许上传的文件后缀
                );
                $upload = new \Think\Upload($config);
                //文件上传保存，返回附件的相关信息
                $up = $upload->uploadOne($_FILES['picture']);
                if ($up) {
                    
                    //旧图片删除
                     $supl = 'http://'.$_SERVER['HTTP_HOST'].'/Show/';
                     $pic = ltrim($list['picture'],$supl);
                     unlink($pic);
                    
                    //图片上传成功后获得图片路径
                    $_POST['picture'] = 'http://' . $_SERVER['HTTP_HOST'] . '/Show/' . $upload->rootPath . $up['savepath'] . $up['savename'];
                }else{
                    $error = $upload -> getError();
                    $this->redirect('xiugai',array('id'=>$id), 2,$error); 
                }
            }
                       
            $shu = $news->create();
            if ($shu) {
                if ($news->save()) {
                    $this->redirect('showlist', '', 2, '修改成功');
                } else {
                    $this->redirect('showlist');
                }
            } else {
                $error = $news->getError();
                $this->alert($error);
            }
        }

        $this->assign('news_class', $arr);
        $this->assign('list', $list);
        $this->display();
    }

    function delete($id) {
        if (is_array($id)) {
            $str = implode(',', $id);
        } else {
            $str = $id;
        }
        $news = new News_contentsModel();
        $supl = 'http://' . $_SERVER['HTTP_HOST'] . '/Show/';

        $res = $news->select($str);

        //删其图片
        foreach ($res as $k => $v) {
            $picture = ltrim($v['picture'], $supl);
            unlink($picture);
        }
        
        //删除数据
        if($news -> delete($str)){
            $this->redirect('showlist');
        }else {
            $this->redirect('showlist');
        }
    }

    function news_class() {
        $class = new News_classModel();
        $arr = $class->select();
        return $arr;
    }

    function id_find($news_id) {
        //id 查找
        $nsmd = new News_contentsModel();
        $news = $nsmd->find($news_id);
        $class = $this->news_class();

        $this->assign('news', $news);
        $this->assign('class', $class);
        $this->assign('news_id', $news_id);
        $this->display();
    }

    function contents_find($contents) {
        //标题查找
        $nsmd = new News_contentsModel();

        $where['title'] = array('like', "%$contents%");
        $list = $nsmd->where($where)->select();
        $zlist = $this->news_class();

        $this->assign('list', $list);
        $this->assign('zlist', $zlist);
        $this->assign('contents', $contents);
        $this->display();
    }

    function check() {
        //查找所有待审核的内容
        $ncmd = new News_contentsModel();

        $zlist = $this->news_class();
        $list = $ncmd->where('state=0')->select();

        $this->assign('list', $list);
        $this->assign('zlist', $zlist);
        $this->display();
    }

    function state_change($news_id, $state) {
        //改变内容的状态
        $ncmd = new News_contentsModel;

        $change = array();
        $change['id'] = $news_id;
        $change['state'] = $state;

        $ncmd->save($change);
    }

    
    function class_check($id,$name){
        //查找栏目id为$id的新闻
        $ncmd =  new News_contentsModel();
        $ncmd -> where("n_id=$id");
        
        //分页
        $total = $ncmd -> count();
        $rows  =  3;
        $page  = new \Tools\Page($total, $rows);
        $limit = $page -> getLimit();
        
        //获取数据
        $ncmd  -> limit($limit);
        $ncmd  -> order('time desc');
        $list  =  $ncmd -> where("n_id=$id") -> select();
        $fpage =  $page -> fpage();
        
        $this -> assign('list',$list);
        $this -> assign('name',$name);
        $this -> assign('page',$fpage);
        $this -> display();
    }
    
    function rejection(){
        //退稿内容列表
        $ncmd = new News_contentsModel();
        
        $admin = session('admin_name');
        
        $list = $ncmd->where("author='{$admin}' AND state=2")->select();

        //所有栏目
        $zlist = $this->news_class();
         
        $this->assign('list', $list);
        $this->assign('zlist', $zlist);
        $this->display();

    }
    
}
